﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Baddies.Utils
{
    /// <summary>
    /// This is a specially prepared Texture2D class to deal with serialization.
    /// On creation of the Texture2DProxy we asign it to a ContentManager, so if the
    /// texture is loaded from xml (only the name) we can always load the actual texture form
    /// the ContentManager. It's got a specific ContentManager assigned instead of requesting one
    /// on the fly from the EventManager because there might be several ContentManagers running
    /// at the same time and we have no idea of which is apropiate to use for the texture.
    /// </summary>
    public class Texture2DProxy
    {
        /// <summary>
        /// Reference to the the parent class that holds a ContentManager to load this texture.
        /// </summary>
        [ContentSerializerAttribute(SharedResource = true)]
        private IContentHolder contentRef;

        /// <summary>
        /// Texture we wrap.
        /// </summary>
        private Texture2D texture;

        /// <summary>
        /// Name of the texture in the ContentManager.
        /// </summary>
        private string name;

        /// <summary>
        /// Initializes a new instance of the Texture2DProxy class.
        /// </summary>
        /// <param name="contentRef">Content manager that will be used for loading.</param>
        public Texture2DProxy(IContentHolder contentRef)
        {
            this.contentRef = contentRef;
            this.name = string.Empty;
            this.texture = null;
        }

        /// <summary>
        /// Initializes a new instance of the Texture2DProxy class.
        /// </summary>
        public Texture2DProxy()
        {
            this.contentRef = null;
            this.name = string.Empty;
            this.texture = null;
        }

        /// <summary>
        /// Gets or sets the name of the texture.
        /// </summary>
        /// <value>Name of the texture.</value>
        public string Name
        {
            set { this.name = value; }
            get { return this.name; }
        }

        /// <summary>
        /// Gets or sets the texture of the proxy.
        /// <remarks>
        /// The Get method has lazy initialization of the texture, in the sense
        /// that if we have the texture name and not the texture, it loads it when we
        /// request the texture. This is useful for when we load a texture via serialization.
        /// </remarks>
        /// </summary>
        /// <value>Texture that we wrap.</value>
        [ContentSerializerIgnore]
        public Texture2D Texture
        {
            get 
            {
                if (this.texture == null)
                {
                    if (this.name != string.Empty)
                    {
                        ContentManager man = this.contentRef.GetContent();
                        this.texture = this.contentRef.GetContent().Load<Texture2D>(this.name);
                    }
                }

                return this.texture; 
            }

            set 
            {
                this.texture = value; 
            }
        }

        /// <summary>
        /// Loads the selected texture.
        /// </summary>
        /// <param name="name">Name of the texture to load.</param>
        public void Load(string name)
        {
            this.name = name;
            this.texture = this.contentRef.GetContent().Load<Texture2D>(name);
        }
    }
}
